package Interpreter_Pattern.Simple;


import java.util.Stack;

// -*-coding:utf-8 -*-
//人员：刘陆鑫
//班级：19软3
//学号；2019110218
public class InstructionHandler {
    private String anInt;
    private AbstractNode abstractNode;

    public void handle(String anInt) {
       AbstractNode left = null, right = null;
        Stack<AbstractNode> stack = new Stack<AbstractNode>();
        String[] words = anInt.split(" ");
        for (int i = 0; i < words.length; i++) {
            if (words[i].equalsIgnoreCase("and")) {
                left = (AbstractNode) stack.pop();
                int word2 = Integer.parseInt(words[++i]);
                right = new NumberNode(word2);
                stack.push(new AndNode(left, right));
            } else if (words[i].equalsIgnoreCase("or")){
                left = (AbstractNode) stack.pop();
                int word2 = Integer.parseInt(words[++i]);
                right = new NumberNode(word2);
                stack.push(new OrNode(left, right));
            } else {
                if (words[i] != "" && !words[i].equals("")) {
                    stack.push(new NumberNode(Integer.parseInt(words[i])));
                }
            }
        }
        this.abstractNode = (AbstractNode) stack.pop();
    }

    public int play() {
        return abstractNode.result();
    }
}
